<?php

class Zend_View_Helper_Cart extends Zend_View_Helper_Abstract
{
    /**
     * @var Storefront_Model_Cart
     */
    public $cartModel;
    
    /**
     * Instantiate a new Cart Model
     *
     * @return Zend_View_Helper_Cart 
     */
    public function Cart()
    {
        $this->cartModel = new Storefront_Model_Cart();
        
        return $this; //fluid interface
    }
    
    /**
     * Creates the html that will be used to display a summary of the cart items
     * and subtotal to the user.
     *
     * @return string 
     */
    public function getSummary()
    {
        $currency = new Zend_Currency();
        $itemCount = count($this->cartModel);
        
        if (0 == $itemCount) {
            return '<p>No items</p>';
        }
        
        $html = '<p>Items: ' . $itemCount;
        $html .= ' | Total: ' . $currency->toCurrency($this->cartModel->getSubTotal());
        $html .= '<br /><a href="';
        $html .= $this->view->url(array(
                'controller'    => 'cart',
                'action'        => 'view',
                'module'        =>'storefront',
            ),
                'default',
                true
        );
        $html .= '">View Cart</a></p>';
        
        return $html;
    }
    
    /**
     * Return a form to adding a single product to the cart
     *
     * @param Storefront_Resource_Product_Item $product
     * @return Storefront_Form_Cart_Add
     */
    public function addForm(Storefront_Resource_Product_Item $product)
    {
        $form = $this->cartModel->getForm('cartAdd');
        $form->populate(array(
            'productId' => $product->productId,
            'returnto'  => $this->view->url(),
        ));
        
        $form->setAction($this->view->url(array(
                'controller'    => 'cart',
                'action'        => 'add',
                'module'        =>'storefront',
            ),
                'default',
                true
        ));
        
        return $form;
    }
    
    /**
     * Return the table containing all cart items
     *
     * @return Storefront_Form_Cart_Table
     */
    public function cartTable()
    {
        $cartTable = $this->cartModel->getForm('cartTable');
        $cartTable->setAction($this->view->url(array(
                'controller'    => 'cart',
                'action'        => 'update',
                'module'        =>'storefront',
            ),
                'default'
        ));
        
        $qtys = new Zend_Form_SubForm();
        
        // 'belongsTo' definisce un parametro array ('quantity') che sarà usato
        // nel controllore 
        foreach ($this->cartModel as $item) {
            $qtys->addElement('text', (string) $item->productId, array(
                'value' => $item->qty,
                'belongsTo' => 'quantity',
                'style' => 'width: 20px;',
                'decorators' => array(
                    'ViewHelper'
                ),
            ));
        }
        
        $cartTable->addSubForm($qtys, 'qtys');
        
        //add shipping elements
        $cartTable->addElement('select', 'shipping', array(
            'decorators' => array(
                'ViewHelper'
            ),
            'MultiOptions' => $this->_getShippingMultioptions(),
            'onChange' => 'this.form.submit();',
            'value' => $this->cartModel->getShippingCost()
        ));
        
        return $cartTable;
    }
    
        /**
     *
     * @param float $amount
     * @return string 
     */
    public function formatAmount($amount)
    {
        $currency = new Zend_Currency;
        return $currency->toCurrency($amount);
    }
    
    /**
     *
     * @return array
     */
    private function _getShippingMultioptions() 
    {
        $currency = new Zend_Currency;
        $shipping = new Storefront_Model_Shipping();
        $options = array(
            0 => 'Please Select',
        );
        
        foreach ($shipping->getShippingOptions() as $key => $value) {
            $options["$value"] = $key . ' - ' . $currency->toCurrency($value);
        }
        
        return $options;
    }
    
}